//
//  HWMyChangeduihuaViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/10/30.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWMyChangeduihuaViewController.h"
#import "HWduiHuanTableViewCell.h"
#import "HWDuihuanModel.h"


@interface HWMyChangeduihuaViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *gameArr;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) NSString *p;
@end

@implementation HWMyChangeduihuaViewController

-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
        
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        _mainaTableVie.backgroundColor = RGB(247, 247, 247);
        _mainaTableVie.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    }
    return   _mainaTableVie ;
}





- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    
    
    
    
    [self setupUI];
    
    
    
}








- (void)setupUI {
    
    
    //   self.gameArr = [[NSMutableArray alloc]init];
    
    self.navigationItem.title  = @"我的兑换";
    
    NSString  *duihuan =     [HWjianzhiduiObject keylocalization:@"我的兑换"];
    self.navigationItem.title  = duihuan;
//    self.navigationItem.title  = [HWjianzhiduiObject keylocalization:@"绑币余额"];
    self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.mainaTableVie];
    
    [self addrefreshControl];
    
    [self.mainaTableVie mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(NavBarHeight);
    }];
    
}





- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
    [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

#pragma  mark --------- 下拉刷新
- (void)underRefresh {
    
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        // 结束刷新
    //        [self.mainaTableVie.mj_header endRefreshing];
    //    });
    
    
    // [SVProgressHUD show];
    
    
    
    self.p = @"1";
    
    __weak typeof(self) weakSelf = self;
    
    self.gameArr = [[NSMutableArray alloc]init];
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    NSString *typeStr =  @"3";
    //        NSDictionary *parametes = @{
    //                                        @"ct" :  @"app",
    //                                        @"ac" : @"mall_order",
    //                                        @"ios" : @"1",
    //                                        @"uid": userID,
    //                                        @"t" :tStr,
    //                                        @"sign": signMD5,
    //                                        @"p" : @"1",
    //                                        @"member_id":[HWLocallyStoredData getCode],
    //
    //
    //                                        };
    
    
    NSDictionary *parametes = @{
        
        @"ios" : @"1",
        @"uid": userID,
        @"t" :tStr,
        @"sign": signMD5,
        @"p" : @"1",
        @"member_id":[HWLocallyStoredData getCode],
        @"language":[HWjianzhiduiObject returnLanguageAcquisition],
        
    };
    
    
    
    [HWWebUtils do4GetWithParameters:parametes hiddenparameterStr:HW_mall_order progress:nil success:^(id  _Nullable response) {
        
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        if ([codeStr intValue] == 1) {
            
            
            
            self.gameArr =   [HWDuihuanModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            self.p = response[@"p"];
            self.max_p = response[@"max_p"];
            [self.mainaTableVie reloadData];
            
            
            
        }
        
        
        
        NSLog(@"%@",response);
        
    } failure:^(NSString *error) {
        
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        
    } ];
    
    
    
    
    
    
    
    
    
    //    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
    //
    //        // [SVProgressHUD dismiss];
    //        [self.mainaTableVie.mj_header endRefreshing];
    //        [self.mainaTableVie.mj_footer endRefreshing];
    //
    //
    //        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
    //
    //        if ([codeStr intValue] == 1) {
    //
    //
    //
    //            self.gameArr =   [HWDuihuanModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
    //            self.p = response[@"p"];
    //            self.max_p = response[@"max_p"];
    //            [self.mainaTableVie reloadData];
    //
    //
    //
    //        }
    //
    //
    //
    //        NSLog(@"%@",response);
    //
    //
    //    } failure:^(NSString *error) {
    //
    //
    //        [self.mainaTableVie.mj_header endRefreshing];
    //        [self.mainaTableVie.mj_footer endRefreshing];
    //
    //
    //    }];
    //
    
    
}




- (void)onRefresh {
    
    
    if ([self.p  intValue] > [self.max_p intValue]) {
        
        [MBProgressHUD  showError:@"请稍后加载数据"];
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        return;
    }
    
    
    
    
    
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    NSString *typeStr =  @"3";
    //      NSDictionary *parametes = @{
    //                                      @"ct" :  @"app",
    //                                      @"ac" : @"mall_order",
    //                                      @"ios" : @"1",
    //                                      @"uid": userID,
    //                                      @"t" :tStr,
    //                                      @"sign": signMD5,
    //                                       @"p" : self.p,
    //                                       @"member_id":[HWLocallyStoredData getCode],
    //
    //
    //                                      };
    
    
    NSDictionary *parametes = @{
        @"ct" :  @"app",
        @"ac" : @"mall_order",
        @"ios" : @"1",
        @"uid": userID,
        @"t" :tStr,
        @"sign": signMD5,
        @"p" : self.p,
        @"member_id":[HWLocallyStoredData getCode],
        @"language":[HWjianzhiduiObject returnLanguageAcquisition],
        
        
    };
    
    
    [HWWebUtils do4GetWithParameters:parametes hiddenparameterStr:HW_mall_order progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        if ([codeStr intValue] == 1) {
            
            //    NSArray *arr1 =  response[@"data"][@"top"];
            
            NSArray *arr2 =  response[@"data"];
            
            
            
            
            NSMutableArray *mutableArr2 =    [HWDuihuanModel mj_objectArrayWithKeyValuesArray:arr2];
            
            
            
            
            for (int  i = 0; i <  mutableArr2 .count ; i++) {
                
                [self.gameArr addObject:mutableArr2[i]];
                
                
            }
            
            
            
            
            self.p = response[@"p"];
            self.max_p = response[@"max_p"];
            
            [self.mainaTableVie reloadData];
            
            
            
            
            
        }
        
        
        
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
    
    
    
    
    
    
    
    
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {
    
    
    return  self.gameArr.count;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    
    HWduiHuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWduiHuanTableViewCell" owner:nil options:nil].firstObject;
        
    }
    
    cell.model =  self.gameArr[indexPath.row];
    
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    
    
    
    return  cell;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return  210;
}

@end
